package org.water.garden.activity.account;

import org.water.garden.R;
import org.water.garden.view.AccountListAdapter;

import android.app.ListActivity;
import android.content.ComponentName;
import android.content.ContentUris;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Im.Account;
import android.view.Menu;
import android.view.View;
import android.view.Menu.Item;
import android.widget.ListAdapter;
import android.widget.ListView;

/**
 * @author sshwsfc@gmail.com
 * Display All im Accounts
 */
public class AccountList extends ListActivity {

	// Menu item Ids
	public static final int DELETE_ID = Menu.FIRST;
	public static final int INSERT_ID = Menu.FIRST + 1;
	
	private Cursor cursor;
	private Uri url;

	@Override
	protected void onCreate(Bundle icicle) {
		super.onCreate(icicle);
		
		setDefaultKeyMode(SHORTCUT_DEFAULT_KEYS);

		Intent intent = getIntent();
		if (intent.getData() == null) {
			intent.setData(Account.CONTENT_URI);
		}
		
		cursor = Account.query(getContentResolver(), new String[]{
			Account._ID, Account.NAME, Account.USERNAME, Account.PROVIDER, Account.ACTIVE
		});
		
		ListAdapter adapter = new AccountListAdapter(this, cursor);
		setListAdapter(adapter);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);

		// Add account button
		Item item = menu.add(0, INSERT_ID, R.string.account_insert);
		item.setShortcut('3', 'a');
		item.setIcon(R.drawable.account_add);

		// For other activitys
		Intent intent = new Intent(null, getIntent().getData());
		intent.addCategory(Intent.ALTERNATIVE_CATEGORY);
		menu.addIntentOptions(Menu.ALTERNATIVE, 0, new ComponentName(this, AccountList.class), null, intent, 0, null);

		return true;
	}

	@Override
	public boolean onPrepareOptionsMenu(Menu menu) {
		super.onPrepareOptionsMenu(menu);
		final boolean haveItems = cursor.count() > 0;

		if (haveItems) {
			// Build menu... always starts with the EDIT action...
			Intent[] specifics = new Intent[1];
			specifics[0] = new Intent(Intent.EDIT_ACTION, Account.CONTENT_URI);
			Menu.Item[] items = new Menu.Item[1];

			// ... is followed by whatever other actions are available...
			Intent intent = new Intent(null, Account.CONTENT_URI);
			intent.addCategory(Intent.SELECTED_ALTERNATIVE_CATEGORY);
			menu.addIntentOptions(Menu.SELECTED_ALTERNATIVE, 0, null, specifics, intent, 0, items);

			// ... and ends with the delete command.
			menu.add(Menu.SELECTED_ALTERNATIVE, DELETE_ID, R.string.account_delete, R.drawable.account_remove).setShortcut('2', 'd');

			// Give a shortcut to the edit action.
			if (items[0] != null) {
				items[0].setShortcut('1', 'e');
			}
		} else {
			menu.removeGroup(Menu.SELECTED_ALTERNATIVE);
		}

		menu.setItemShown(DELETE_ID, haveItems);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(Menu.Item item) {
		switch (item.getId()) {
		case DELETE_ID:
			deleteAccount();
			return true;
		case INSERT_ID:
			insertItem();
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {		
		url = ContentUris.withAppendedId(Account.CONTENT_URI, id);

		String action = getIntent().getAction();
		if (Intent.PICK_ACTION.equals(action) || Intent.GET_CONTENT_ACTION.equals(action)) {
			setResult(RESULT_OK, url.toString());
		} else {
			Intent menuAccountInt = new Intent(this, AccountMenu.class);	
			menuAccountInt.setData(url);
			startActivity(menuAccountInt);
		}
	}
	
	private final void deleteAccount() {
		if(getSelectedItemPosition() >= 0){
			cursor.moveTo(getSelectedItemPosition());
			cursor.deleteRow();
		}
	}

	private final void insertItem() {
		Intent addAccountInt = new Intent(this, AccountAdd.class);		
		startActivity(addAccountInt);
	}
	
}
